package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author chengjy
 * @date 2023/4/20
 */
@RestController
@Slf4j
@RequestMapping("/user/shoppingCart")
@Api(tags = "微信小程序购物车模块接口")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 新增购物车
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加购物车")
    public Result<String> add(@RequestBody ShoppingCartDTO shoppingCartDTO){
        shoppingCartService.add(shoppingCartDTO);
        log.info("添加购物车：{}",shoppingCartDTO);
        return Result.success("添加成功");
    }


    /**
     * 查询购物车
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询购物车")
    public Result<List<ShoppingCart>> getByUserId(){
        List<ShoppingCart> list = shoppingCartService.getByUserId();
        log.info("查询购物车：{}", BaseContext.getCurrentId());
        return Result.success(list);
    }
    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    @ApiOperation("清空购物车")
    public Result<String> clean(){
        shoppingCartService.clean();
        log.info("清空购物车：{}", BaseContext.getCurrentId());
        return Result.success("已清空，买不起的穷B！！");
    }

    /**
     * 减少购物车商品
     * @return
     */
    @PostMapping("/sub")
    @ApiOperation("减少购物车商品")
    public Result<String> updateShoppingCart(@RequestBody ShoppingCartDTO shoppingCartDTO){
        shoppingCartService.updateShoppingCart(shoppingCartDTO);
        log.info("减少购物车商品：{}", shoppingCartDTO);
        return Result.success("已清空，买不起的穷B！！");
    }




}
